package com.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

public class JDBCDemo {

    public static void showStudents(){
        List<Student> list = JDBCUtils.query(Student.class,
                "select * from student");
        for(Student s : list){
            System.out.println(s);
        }
    }

        //使用数据源工具类获取链接
//        try(Connection connection = DataSourceUtils.getConnection()){
//            PreparedStatement ps = connection.prepareStatement("select * from student");
//           ResultSet rs = ps.executeQuery();
//            while (rs.next()){
//                int id = rs.getInt("id");
//                String name = rs.getString("name");
//                int age = rs.getInt("age");
//                System.out.println("id"+id+" name:"+name+" age"+age);
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
    //实现学生搜索，输入字符，只要名字包含输入字符的都显示出来
    /*public  static  void searchStudents(String sname){
        try(Connection conn = DataSourceUtils.getConnection()){
            PreparedStatement ps = conn.prepareStatement("select * from student where name like ?");
            ps.setString(1,"%"+sname+"%");
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                System.out.println("id"+id+" name:"+name+" age"+age);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
*/
        public static void searchStudents(String sname){
            List<Student> list = JDBCUtils.query(Student.class,
                    "select * from student where name like ?",
                    "%" + sname +"%");
            for(Student s : list){
                System.out.println(s);
            }
        }


    public  static  void main(String[] args){
        showStudents();
 //       searchStudents("三");
    }
}

